/******************************************************************
******** 
 * ++ 
 * Author:    Team 1
 * Module Name  :  Board.cpp
 * 
 * Description  : Source file for class Board
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 
 *                
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 
 * 
 * Mod. History :  18.06.13 - Truong Cong Hau
 *                            File first created 
 *                            MP PROJECT 
 *							  Initialize, control and manage main function of program
 * Mod. History :  18.06.13 - Dang Chi Cong
 *                            Modified File
 *                            MP PROJECT 
 *							  Modified Board function
 *							  Modified SetBoard function
 *							  Modified Board function
 *							  Modified IsLegitClick function
 *							  Modified InitializeChessPiece function
 *							  Modified ClearUp function
 *							  Modified MovePiece function
 *							  Modified HasPieceAt function
 *							  Modified Click function
 * --		
 
*******************************************************************
*****/

#include <sstream>
#include <typeinfo>
//Include files
#include "stdafx.h"
#include "Board.h"
#include "ChariotPiece.h"
#include "HorsePiece.h"
#include "AdvisorPiece.h"
#include "CannonPiece.h"
#include "ElephantPiece.h"
#include "SoliderPiece.h"
#include "GeneralPiece.h"
#include "BasicRule.h"
#include "FileUtility.h"
#include <tchar.h>

/**********************************************************
********
*++
* Method name : Board::Board ()
* Decription  : This method used to
* 
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
*
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
* Parameter : no
* Global available : no
* Return value : no
*               
*--
***********************************************************
********/
Board::Board(void)
{
	/*this->SetBoard(std::string(ConstValue::pixDir + "ixiangqi.png"));
	this->InitializeChessPiece();*/
	//basicRule->SetBoard(m_plsBoardCell);
	BasicRule::SetBoard(this);
	//plszLstLog = NULL;
	//BasicRule::SetListLog(plszLstLog);
	players = Player();
}

/**********************************************************
********
*++
* Method name : Board::~Board ()
* Decription  : This method used to 
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
*
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
* Parameter : no
* Global available : no
* Return value : no
*               
*--
***********************************************************
********/
Board::~Board(void)
{
}

/**********************************************************
********
*++
* Method name : Board::Draw ()
* Decription  : This method used to 
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
*
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
* Parameter : graphics
* Global available : no
* Return value : no
*               
*--
***********************************************************
********/
void Board::Draw(Gdiplus::Graphics &graphics)
{
	graphics.DrawImage(m_pImage, ConstValue::xBeginDraw, ConstValue::yBeginDraw);
	for(int x=0; x<BoardInfo::piecesEachSide * BoardInfo::sideCount; x++)
	//int x=0;
	//do{
	//{
		if(TRUE == m_plsPiece[x]->IsActive())
		{
			m_plsPiece[x]->Update();
			m_plsPiece[x]->Draw(graphics);
		}
	//}
	//x=x+8;
	//}
	//while(x<25);
}

/**********************************************************
********
*++
* Method name : Board::SetBoard()
* Decription  : This method used to set board image
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
*
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
* Parameter : boardDir
* Global available : no
* Return value : no
*               
*--
***********************************************************
********/
void Board::SetBoard(std::string boardDir)
{
	wchar_t* wFileDir = FileUtility::GetInstance()->ConvertToWcharT(boardDir);
	m_pImage.Attach(new Gdiplus::Image(wFileDir));
}

/**********************************************************
********
*++
* Method name : Board::IsLegitClick ()
* Decription  : This method used to 
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
*
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
* Parameter : position
* Global available : no
* Return value : bool
*               
*--
***********************************************************
********/
bool Board::IsLegitClick(POINT position)
{
	int x = position.x - ConstValue::xBeginDraw;
	int y = position.y - ConstValue::yBeginDraw;
	if(0<x && 0<y)
	{
		int col = y/ ConstValue::cellHeightWidth;
		int row = x/ ConstValue::cellHeightWidth;
		if(col<BoardInfo::col && row<BoardInfo::row)
		{
			return TRUE;
		}
	}
	return FALSE;
}

/**********************************************************
********
*++
* Method name : Board::InitializeChessPiece ()
* Decription  : This method used to 
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
*
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
* Parameter : no
* Global available : no
* Return value : no
*               
*--
***********************************************************
********/
void Board::InitializeChessPiece()
{
	for(int indexX = 0; indexX < BoardInfo::row; indexX++)
	{
		for(int indexY = 0; indexY < BoardInfo::col; indexY++)
		{
			m_plsBoardCell[indexX][indexY] = NULL;
		}
	}

	for(int x=0; x<BoardInfo::piecesEachSide * BoardInfo::sideCount; x++)
	{
		m_plsPiece[x]= NULL;
		
	}
	//Initialize the red side piece
	int row = 0, collumn = 0, index =0;
	//Draw tower
	//row = 0, collumn = 0

	m_plsPiece[index] = new ChariotPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "r_ju.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "r_ju_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(true);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//Draw knigth
	//row = 0, collumn = 1
	collumn++;
	index++;
	m_plsPiece[index] = new HorsePiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "r_ma.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "r_ma_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(true);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//Draw elephant
	//row = 0, collumn = 2
	collumn++;
	index++;
	m_plsPiece[index] = new ElephantPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "r_xiang.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "r_xiang_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(true);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//Draw general bodyguard
	//row = 0, collumn = 3
	collumn++;
	index++;
	m_plsPiece[index] = new AdvisorPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "r_shi.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "r_shi_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(true);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//Draw general
	//row = 0, collumn = 4
	collumn++;
	index++;
	m_plsPiece[index] = new GeneralPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "r_gong.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "r_gong_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(true);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	BasicRule::SetRedKing(m_plsPiece[index]);
	//Draw general bodyguard
	//row = 0, collumn = 5
	collumn++;
	index++;
	m_plsPiece[index] = new AdvisorPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "r_shi.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "r_shi_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(true);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//Draw elephant
	//row = 0, collumn = 6
	collumn++;
	index++;
	m_plsPiece[index] = new ElephantPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "r_xiang.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "r_xiang_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(true);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//Draw knight
	//row = 0, collumn = 7
	collumn++;
	index++;
	m_plsPiece[index] = new HorsePiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "r_ma.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "r_ma_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(true);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//Draw tower
	//row = 0, collumn = 8
	collumn++;
	index++;
	m_plsPiece[index] = new ChariotPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "r_ju.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "r_ju_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(true);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//Draw artillery
	//row = 2, collumn =1
	row=2, collumn =1;
	index++;
	m_plsPiece[index] = new CannonPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "r_pao.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "r_pao_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(true);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//row = 2, collumn = 7
	collumn = 7;
	index++;
	m_plsPiece[index] = new CannonPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "r_pao.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "r_pao_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(true);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//Draw spawn
	//row =3, collumn = 0
	row = 3, collumn = 0;
	index++;
	m_plsPiece[index] = new soliderPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "r_zu.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "r_zu_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(true);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//row =3, collumn = 2
	collumn = 2;
	index++;
	m_plsPiece[index] = new soliderPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "r_zu.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "r_zu_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(true);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//row =3, collumn = 4
	collumn = 4;
	index++;
	m_plsPiece[index] = new soliderPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "r_zu.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "r_zu_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(true);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//row =3, collumn = 6
	collumn = 6;
	index++;
	m_plsPiece[index] = new soliderPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "r_zu.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "r_zu_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(true);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//row =3, collumn = 8
	collumn = 8;
	index++;
	m_plsPiece[index] = new soliderPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "r_zu.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "r_zu_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(true);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	/*
	 *
	 *
	 */
	//Initialize the blue side piece
	row = BoardInfo::row-1, collumn = 0;
	index++;
	//Draw tower
	//row = 0, collumn = 0
	m_plsPiece[index] = new ChariotPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "b_ju.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "b_ju_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(false);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//Draw knight
	//row = 0, collumn = 1
	collumn++;
	index++;
	m_plsPiece[index] = new HorsePiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "b_ma.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "b_ma_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(false);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//Draw elephant
	//row = 0, collumn = 2
	collumn++;
	index++;
	m_plsPiece[index] = new ElephantPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "b_xiang.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "b_xiang_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(false);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//Draw general bodyguard
	//row = 0, collumn = 3
	collumn++;
	index++;
	m_plsPiece[index] = new AdvisorPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "b_shi.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "b_shi_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(false);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//Draw general
	//row = 0, collumn = 4
	collumn++;
	index++;
	m_plsPiece[index] = new GeneralPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "b_gong.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "b_gong_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(false);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	BasicRule::SetBlackKing(m_plsPiece[index]);
	//Draw general bodyguard
	//row = 0, collumn = 5
	collumn++;
	index++;
	m_plsPiece[index] = new AdvisorPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "b_shi.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "b_shi_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(false);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//Draw elephant
	//row = 0, collumn = 6
	collumn++;
	index++;
	m_plsPiece[index] = new ElephantPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "b_xiang.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "b_xiang_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(false);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//Draw knight
	//row = 0, collumn = 7
	collumn++;
	index++;
	m_plsPiece[index] = new HorsePiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "b_ma.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "b_ma_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(false);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//Draw tower
	//row = 0, collumn = 8
	collumn++;
	index++;
	m_plsPiece[index] = new ChariotPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "b_ju.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "b_ju_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(false);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//Draw artillery
	//row = 7, collumn =1
	row=7, collumn =1;
	index++;
	m_plsPiece[index] = new CannonPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "b_pao.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "b_pao_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(false);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//row = 7, collumn = 7
	collumn = 7;
	index++;
	m_plsPiece[index] = new CannonPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "b_pao.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "b_pao_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(false);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//Draw spawn
	//row =3, collumn = 0
	row = 6, collumn = 0;
	index++;
	m_plsPiece[index] = new soliderPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "b_zu.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "b_zu_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(false);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//row =6, collumn = 2
	collumn = 2;
	index++;
	m_plsPiece[index] = new soliderPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "b_zu.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "b_zu_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(false);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//row =6, collumn = 4
	collumn = 4;
	index++;
	m_plsPiece[index] = new soliderPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "b_zu.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "b_zu_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(false);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//row =6, collumn = 6
	collumn = 6;
	index++;
	m_plsPiece[index] = new soliderPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "b_zu.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "b_zu_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(false);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];
	//row =6, collumn = 8
	collumn = 8;
	index++;
	m_plsPiece[index] = new soliderPiece();
	m_plsPiece[index]->SetImage(std::string(ConstValue::pixDir + "b_zu.png"));
	m_plsPiece[index]->SetSelectedImage(std::string(ConstValue::pixDir + "b_zu_h.png"));
	m_plsPiece[index]->SetRow(row);
	m_plsPiece[index]->SetCollumn(collumn);
	m_plsPiece[index]->SetRedSide(false);
	m_plsBoardCell[row][collumn] = m_plsPiece[index];

	/*basicRule = new BasicRule();
	basicRule->SetBoard(m_plsBoardCell);*/
}

/**********************************************************
********
*++
* Method name : Board::ClearUp ()
* Decription  : This method used to 
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
*
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
* Parameter : no
* Global available : no
* Return value : no
*               
*--
***********************************************************
********/
void Board::ClearUp()
{
	for(int x=0; x<BoardInfo::row; x++)
	{
		if(NULL!= this->m_plsBoardCell)
		{
			delete[] m_plsBoardCell;
		}
	}
	if(NULL!= this->m_plsPiece)
	{
		delete[] m_plsPiece;
	}
	if(NULL != m_pChosenPiece)
	{
		delete m_pChosenPiece;
	}

	m_pImage.Free();
	free(m_pImage);

}

/**********************************************************
********
*++
* Method name : Board::MovePiece ()
* Decription  : This method used to 
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
*
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
* Parameter : position
* Global available : no
* Return value : no
*               
*--
***********************************************************
********/
void Board::MovePiece(HWND hListBoxControl,POINT position)
{
	
	bool bCanMove = FALSE;
	POINT pointOldPosition = POINT();
	int oss;
	WCHAR  numberMove[10];
		
	//If chosen piece is null and there ie a piece at the chosen position, set chosen piece to the piece at that position 
	//and then change that piece's state
	if(NULL == m_pChosenPiece)
	{
		if(NULL!=m_plsBoardCell[position.x][position.y] && m_plsBoardCell[position.x][position.y]->IsRedSide() == players.GetCurrentTurn())
		{
			m_pChosenPiece = m_plsBoardCell[position.x][position.y];
			m_pChosenPiece->ChangeSelectedState();
		}
	}
	else
	{
		//If the chosen piece is chosen again, change selected state and point chosenPiece to NULL
		if(m_pChosenPiece->GetCollumn() == position.y && m_pChosenPiece->GetRow() == position.x)
		{
			m_pChosenPiece->ChangeSelectedState();
			m_pChosenPiece = NULL;
		}
		else
		{
			pointOldPosition.y = m_pChosenPiece->GetCollumn();
			pointOldPosition.x = m_pChosenPiece->GetRow();
			//Check if the chosen positon is one of the possible move and the move is legit
			if(true == m_pChosenPiece->CanMoveTo(pointOldPosition, position))
			{
						
				if(NULL!=m_plsBoardCell[position.x][position.y] )
				{
					if(m_plsBoardCell[position.x][position.y]->IsRedSide() != m_pChosenPiece->IsRedSide())
						if (m_pChosenPiece->CanMoveTo(pointOldPosition,position))
								bCanMove = true;
				}
				else if(NULL== m_plsBoardCell[position.x][position.y])
				{
					if (m_pChosenPiece->CanMoveTo(pointOldPosition,position))
						bCanMove = true;
				}
			}					
		}
	}

	if(true == bCanMove)
	{
		//Point to the chosen position
		ChessPiece *cpCapturedPiece = m_plsBoardCell[position.x][position.y];
		//If there is a piece at the chosen position, change piece active state at the chosen position
		if(NULL != m_plsBoardCell[position.x][position.y])
		{
			m_plsBoardCell[position.x][position.y]->SetDeactive();
		}
		//Get both side king
		//
		//Point board new position to the piece address
		m_plsBoardCell[position.x][position.y] = m_plsBoardCell[m_pChosenPiece->GetRow()][m_pChosenPiece->GetCollumn()];
		//Set board old position to null
		m_plsBoardCell[m_pChosenPiece->GetRow()][m_pChosenPiece->GetCollumn()] = NULL;
		//Assign current chosen piece position to the pointOldPosition
		//Assign new position to the current chosen piece's position
		m_pChosenPiece->SetCollumn(position.y);
		m_pChosenPiece->SetRow(position.x);

		//SendMessage(hListBoxControl, LB_ADDSTRING, 0, (LPARAM)(LPCTSTR)TEXT("Item3"));
		//If game rule make this move invalid
		if(true == BasicRule::IsKingFaceKing() || true == BasicRule::IsKingBeingChecked(players.GetCurrentTurn()))
		{
			//Point board old position to the piece address
			m_plsBoardCell[pointOldPosition.x][pointOldPosition.y] = m_plsBoardCell[position.x][position.y];
			m_pChosenPiece->SetCollumn(pointOldPosition.y);
			m_pChosenPiece->SetRow(pointOldPosition.x);
			if(NULL != cpCapturedPiece)
			{
				m_plsBoardCell[position.x][position.y] = cpCapturedPiece;
				cpCapturedPiece->SetActive();
			}
			cpCapturedPiece= NULL;
			delete cpCapturedPiece;
			
		}
		else
		{
			if(TRUE == m_pChosenPiece->IsRedSide())
			{
				//
				oss = FileUtility::GetInstance()->numberMove ++ ;	
				_itow_s(oss,numberMove,10);
				//
				SendMessage(hListBoxControl, LB_ADDSTRING, 0, (LPARAM)numberMove);
				//
				m_pChosenPiece->WriteLogPieceTurn(hListBoxControl,m_pChosenPiece->IsRedSide(),pointOldPosition,position);
			}
			else
			{
				m_pChosenPiece->WriteLogPieceTurn(hListBoxControl,m_pChosenPiece->IsRedSide(),pointOldPosition,position);
			}
			players.ChangePlayer();
		}
		//Unchosen the current chosen piece
		if(true == m_pChosenPiece->IsSelected())
		{
			m_pChosenPiece->ChangeSelectedState();
		}
		//m_pChosenPiece->WriteLogPieceTurn(hListBoxControl,m_pChosenPiece->IsRedSide(),pointOldPosition,position);
		m_pChosenPiece = NULL;
		//Check if there is no next move exist
		if(FALSE == this->DoesNextMoveExist())
		{
			//If a king is being checked
			//
				//Change game state and show message
				//
			//Else
			//
				//Change game state and show message
				//
		}
	}
}

/**********************************************************
********
*++
* Method name : Board::WriteLog ()
* Decription  : This method used to 
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
*
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
* Parameter : no
* Global available : no
* Return value : no
*               
*--
***********************************************************
********/
void Board::WriteLog()
{

}

/**********************************************************
********
*++
* Method name : Board::CreateNewGame ()
* Decription  : This method used to 
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
*
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
* Parameter : no
* Global available : no
* Return value : no
*               
*--
***********************************************************
********/
void Board::CreateNewGame()
{
}

/**********************************************************
********
*++
* Method name : Board::DoesNextMoveExist ()
* Decription  : This method used to 
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
*
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
* Parameter : no
* Global available : no
* Return value : bool
*               
*--
***********************************************************
********/
bool Board::DoesNextMoveExist()
{
	return TRUE;
}

/**********************************************************
********
*++
* Method name : Board::GetGameState ()
* Decription  : This method used to 
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
*
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
* Parameter : no
* Global available : no
* Return value : no
*               
*--
***********************************************************
********/
void Board::GetGameState()
{
}

/**********************************************************
********
*++
* Method name : Board::HasPieceAt ()
* Decription  : This method used to 
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
*
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
* Parameter : position
* Global available : no
* Return value : bool
*               
*--
***********************************************************
********/
bool Board::HasPieceAt(POINT position)
{	
	if(NULL != m_plsBoardCell[position.x][position.y])
		//if(TRUE == m_plsBoardCell[position.x][position.y]->IsActive())
			return TRUE;
	return FALSE;
}

/**********************************************************
********
*++
* Method name : Board::HasPieceAt ()
* Decription  : This method used to 
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
*
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
* Parameter : posX, posY
* Global available : no
* Return value : bool
*               
*--
***********************************************************
********/
bool Board::HasPieceAt(int posX, int posY)
{
	if(NULL != m_plsBoardCell[posX][posY])
		//if(TRUE == m_plsBoardCell[position.x][position.y]->IsActive())
			return TRUE;
	return FALSE;
}

/**********************************************************
********
*++
* Method name : Board::Click ()
* Decription  : This method used to 
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
*
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
* Parameter : position
* Global available : no
* Return value : no
*               
*--
***********************************************************
********/
void Board::Click(HWND hListBox,POINT position)
{
	if(TRUE == this->IsLegitClick(position) )
	{
		POINT boardPosition = POINT();
		boardPosition.x = (position.x - ConstValue::xBeginDraw)/ConstValue::cellHeightWidth;
		boardPosition.y = (position.y - ConstValue::yBeginDraw)/ConstValue::cellHeightWidth;
		this->MovePiece(hListBox,boardPosition);
	}
}

void Board::MoveAuto()
{
	
	//POINT currPos; POINT newPos;
	//int oss = FileUtility::GetInstance()->numberMove ++ ;
	//if(oss<10)
	//{
	//		currPos.x = currPos.y = oss;
	//		newPos.y= currPos.x; 
	//		newPos.y = currPos.y;
	//}

	//m_pChosenPiece = m_plsBoardCell[currPos.x][currPos.y];
	//Sleep(3000);
	//m_pChosenPiece->ChangeSelectedState();

	//ChessPiece *cpCapturedPiece = m_plsBoardCell[newPos.x][newPos.y];
	////If there is a piece at the chosen position, change piece active state at the chosen position
	//if(NULL != m_plsBoardCell[newPos.x][newPos.y])
	//{
	//	m_plsBoardCell[newPos.x][newPos.y]->SetDeactive();
	//}
	////Get both side king
	////
	////Point board new position to the piece address
	//m_plsBoardCell[newPos.x][newPos.y] = m_plsBoardCell[currPos.x][currPos.y];
	////Set board old position to null
	//m_plsBoardCell[currPos.x][currPos.y] = NULL;
	////Assign current chosen piece position to the pointOldPosition
	////Assign new position to the current chosen piece's position
	//m_pChosenPiece->SetCollumn(newPos.y);
	//m_pChosenPiece->SetRow(newPos.x);
	//m_pChosenPiece->ChangeSelectedState();
	//m_pChosenPiece=NULL;
	
}

//int Board::CountNumberPieceSameTypeInColumn(char* nameclass, int col)
//{
//	int i;
//	int count =0;
//	for(i=0 ; i< BoardInfo::row; i++)
//		if(nameclass ==typeid(m_plsBoardCell[i][col]).name())
//			count++;
//	return count;
//}